import { Data } from "../../../modeal/data.js"
import { User } from "../../../modeal/user.js"
const data = new Data()
const user = new User()
Page({
  data: {
    slider: [], //slider
    page: 1,
    goods: [],
    isMore: false
  },

  onLoad: function (options) {
    let userInfo = wx.getStorageSync('userinfo')
    this.setData({ userInfo })

    this.getIntegralGoods();
    this.getIntegralSlider();
  },

  onReady: function () { },


  onShow: function () { },
  onReachBottom: function () {
    this.getIntegralGoods();
  },

  // 获取积分slider
  getIntegralSlider () {
    data.getIntegralSlider().then(res => {
      this.setData({ slider: res });
    })
  },

  // 获取商品
  getIntegralGoods () {
    let page = this.data.page
    data.getIntegralGoods(page).then(res => {
      if (res.length >= 1) {
        this.setData({
          goods: this.data.goods.concat(res),
          page: page + 1,
          isMore: true,
        })
        res.length < 10 ? this.setData({ isMore: false }) : ''
      }
    })
  },

  // 兑换记录
  goRecord(){
    wx.navigateTo({url:"/pages/integralShop/record/record"})
  },

  // 兑换商品
  exchange (e) {
    let id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '积分商城',
      content: '您确认兑换商品?',
      success: res => {
        res.confirm ? this._exchange(id) : ''
      }
    })
  },

  // 兑换
  _exchange (id) {
    user.postExchange(id).then(res => {
      wx.showModal({
        content: "兑换成功",
        showCancel: false
      });

    })
  },

})